﻿using AmazonMODEL;
using MODEL;
using MWSRecommendationsSectionService.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SysController
{
    public static class RecommendationsController
    {
        public static void GetLastUpdatedTimeForRecommendations(AppKey appkey)
        {
            MWSRecommendationsSectionService.MWSRecommendationsSectionServiceClient client = HelperController.RegisterRecommendationsService(appkey);
            //GetLastUpdatedTimeForRecommendationsRequest request = new GetLastUpdatedTimeForRecommendationsRequest()
            //{
            //     MarketplaceId=appkey.MarketplaceCode,
            //     // SellerId=
            //};
            //CategoryQuery model = new CategoryQuery()
            //{
            //    FilterOptions = new List<string>() { "DVD" },
            //    RecommendationCategory = "Selection"
            //};
            ListRecommendationsRequest request = new ListRecommendationsRequest()
            {
                RecommendationCategory = "Fulfillment", 
                MarketplaceId=appkey.MarketplaceCode,
                SellerId=appkey.MerchantCode
            };
            //request.CategoryQueryList = new List<CategoryQuery>();
            //request.CategoryQueryList.Add(model);
            try
            {
                ListRecommendationsResponse response = client.ListRecommendations(request);
                if (response.IsSetListRecommendationsResult())
                {
                    ListRecommendationsResult result = response.ListRecommendationsResult;
                    string str = JsonConvert.SerializeObject(result);
                }
            }
            catch (Exception)
            {
                
            }
            
        }

        public static void ListRecommendations(AppKey appkey, RecommendationCategory type)
        {
            MWSRecommendationsSectionService.MWSRecommendationsSectionServiceClient client = HelperController.RegisterRecommendationsService(appkey);
            ListRecommendationsRequest request = new ListRecommendationsRequest()
            {
                RecommendationCategory = type.ToString(),
                MarketplaceId = appkey.MarketplaceCode,
                SellerId = appkey.MerchantCode
            };
            try
            {
                ListRecommendationsResponse response = client.ListRecommendations(request);
                if (response.IsSetListRecommendationsResult())
                {
                    RecommendationModel model = new RecommendationModel();
                    ListRecommendationsResult result = response.ListRecommendationsResult;
                    //string str = JsonConvert.SerializeObject(result); 
                    switch (type)
                    {
                        case RecommendationCategory.Inventory:
                            List<MODEL.InventoryRecommendation> listInventory = new List<MODEL.InventoryRecommendation>();
                            foreach (var item in result.InventoryRecommendations)
                            {
                                #region InventoryRecommendation
                                MODEL.InventoryRecommendation it = new MODEL.InventoryRecommendation()
                                                        {
                                                            AvailableQuantity = (int)item.AvailableQuantity,
                                                            CreateTime = DateTime.Now,
                                                            CreateUser = "ListRecommendations",
                                                            DaysOutOfStockLast30Days = (int)item.DaysOutOfStockLast30Days,
                                                            DaysUntilStockRunsOut = (int)item.DaysOutOfStockLast30Days,
                                                            FulfillmentChannel = item.FulfillmentChannel,
                                                            InboundQuantity = (int)item.InboundQuantity,
                                                            IsDel = false,
                                                            ItemIdentifierASIN = item.ItemIdentifier.Asin,
                                                            ItemIdentifierSKU = item.ItemIdentifier.Sku,
                                                            ItemIdentifierUPC = item.ItemIdentifier.Upc,
                                                            ItemName = item.ItemName,
                                                            LostSalesInLast30Days = (int)item.LostSalesInLast30Days,
                                                            RecommendationId = item.RecommendationId,
                                                            RecommendationReason = item.RecommendationReason,
                                                            RecommendedInboundQuantity = (int)item.RecommendedInboundQuantity,
                                                            SalesForTheLast14Days = (int)item.SalesForTheLast14Days,
                                                            SalesForTheLast30Days = (int)item.SalesForTheLast30Days,
                                                            LastUpdated = item.LastUpdated
                                                        }; 
                                #endregion
                                listInventory.Add(it);
                            }
                            if (listInventory.Count > 0 || listInventory != null)
                            {
                                model.IsSetInventory = true;
                                model.InventoryModel = listInventory;
                            }
                            else
                            {
                                model.IsSetInventory = false;
                                model.InventoryModel = null;
                            }

                            break;
                        case RecommendationCategory.Pricing:
                            List<MODEL.PricingRecommendation> listPricing = new List<MODEL.PricingRecommendation>();
                            foreach (var item in result.PricingRecommendations)
                            {
                                #region PricingRecommendation
                                MODEL.PricingRecommendation it = new MODEL.PricingRecommendation()
                                                        {
                                                            Condition = item.Condition,
                                                            CreateTime = DateTime.Now,
                                                            CreateUser = "ListRecommendations",
                                                            FulfillmentChannel = item.FulfillmentChannel,
                                                            IsDel = false,
                                                            ItemIdentifierASIN = item.ItemIdentifier.Asin,
                                                            ItemIdentifierSKU = item.ItemIdentifier.Sku,
                                                            ItemIdentifierUPC = item.ItemIdentifier.Upc,
                                                            ItemName = item.ItemName,
                                                            LastUpdated = item.LastUpdated,
                                                            NumberOfAmazonFulfilledOffers = (int)item.NumberOfAmazonFulfilledOffers,
                                                            NumberOfMerchantFulfilledOffers = (int)item.NumberOfMerchantFulfilledOffers,
                                                            NumberOfOffers = (int)item.NumberOfOffers,
                                                            RecommendationId = item.RecommendationId,
                                                            RecommendationReason = item.RecommendationReason,
                                                            SubCondition = item.SubCondition
                                                        };
                                if (item.IsSetYourPricePlusShipping())
                                {
                                    it.YourPricePlusShippingAmount = item.YourPricePlusShipping.Amount;
                                    it.YourPricePlusShippingCurrencyCode = item.YourPricePlusShipping.CurrencyCode;
                                }

                                if (item.IsSetPriceDifferenceToLowPrice())
                                {
                                    it.PriceDifferenceToLowPriceAmount = item.PriceDifferenceToLowPrice.Amount;
                                    it.PriceDifferenceToLowPriceCurrencyCode = item.PriceDifferenceToLowPrice.CurrencyCode;
                                }

                                if (item.IsSetMedianPricePlusShipping())
                                {
                                    it.MedianPricePlusShippingAmount = item.MedianPricePlusShipping.Amount;
                                    it.MedianPricePlusShippingCurrencyCode = item.MedianPricePlusShipping.CurrencyCode;
                                }

                                if (item.IsSetLowestPricePlusShipping())
                                {
                                    it.LowestPricePlusShippingAmount = item.LowestPricePlusShipping.Amount;
                                    it.LowestPricePlusShippingCurrencyCode = item.LowestPricePlusShipping.CurrencyCode;
                                }

                                if (item.IsSetLowestMerchantFulfilledOfferPrice())
                                {
                                    it.LowestMerchantFulfilledOfferPriceAmount = item.LowestMerchantFulfilledOfferPrice.Amount;
                                    it.LowestMerchantFulfilledOfferPriceCurrencyCode = item.LowestMerchantFulfilledOfferPrice.CurrencyCode;
                                }

                                if (item.IsSetLowestAmazonFulfilledOfferPrice())
                                {
                                    it.LowestAmazonFulfilledOfferPriceAmount = item.LowestAmazonFulfilledOfferPrice.Amount;
                                    it.LowestAmazonFulfilledOfferPriceCurrencyCode = item.LowestAmazonFulfilledOfferPrice.CurrencyCode;
                                }
                                #endregion
                                listPricing.Add(it); 
                            }
                            if (listPricing.Count > 0 || listPricing != null)
                            {
                                model.IsSetPricing = true;
                                model.PricingModel = listPricing;
                            }
                            else
                            {
                                model.IsSetPricing = false;
                                model.PricingModel = null;
                            } 
                            break;
                        case RecommendationCategory.Selection:
                             

                            break;
                        case RecommendationCategory.Fulfillment:
                            break;
                        case RecommendationCategory.ListingQuality:
                            break;
                        case RecommendationCategory.GlobalSelling:
                            break;
                        case RecommendationCategory.Advertising:
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception)
            {

            }
        }
    }
}
